﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{

	public class BilldetailDAO:IData
 	{
		public BilldetailDAO(String myconnecString) : base(myconnecString) { }

		public  int OnSubmit(Billdetail item, int t) {

            int i=
			  connec.ExecuteNonQuery("sp_Billdetail", CommandType.StoredProcedure,new SqlParameter("@flag", t),
				 new SqlParameter("@" + Billdetail.c_id, item.id), new SqlParameter("@" + Billdetail.c_billId, item.billId),
				 new SqlParameter("@" + Billdetail.c_productId, item.productId), new SqlParameter("@" + Billdetail.c_numberOf, item.numberOf),
				 new SqlParameter("@" + Billdetail.c_unitPriceUSD, item.unitPriceUSD), new SqlParameter("@" + Billdetail.c_rate, item.rate),
				 new SqlParameter("@" + Billdetail.c_unitPriceVND, item.unitPriceVND), new SqlParameter("@" + Billdetail.c_discount, item.discount),
				 new SqlParameter("@" + Billdetail.c_cash, item.cash), new SqlParameter("@" + Billdetail.c_orderId, item.orderId));
            return i;
		}

		public  Billdetail GetItem(IDataReader dr)
		{
			Billdetail obj = new Billdetail();
			obj.id = Convert.ToInt64(dr[Billdetail.c_id]);
			obj.billId = Convert.ToInt64(dr[Billdetail.c_billId]);
			obj.productId = Convert.ToInt32(dr[Billdetail.c_productId]);
			obj.numberOf = Convert.ToInt32(dr[Billdetail.c_numberOf]);
			obj.unitPriceUSD = Convert.ToDecimal(dr[Billdetail.c_unitPriceUSD]);
			obj.rate = Convert.ToDecimal(dr[Billdetail.c_rate]);
			obj.unitPriceVND = Convert.ToDecimal(dr[Billdetail.c_unitPriceVND]);
			obj.discount = Convert.ToDecimal(dr[Billdetail.c_discount]);
			obj.cash = Convert.ToDecimal(dr[Billdetail.c_cash]);
			obj.orderId = Convert.ToInt32(dr[Billdetail.c_orderId]);
			return obj;
		}

        public Billdetail Item(IDataReader dr)
        {
            Billdetail obj = new Billdetail();
            obj.id = Convert.ToInt64(dr[Billdetail.c_id]);
            obj.billId = Convert.ToInt64(dr[Billdetail.c_billId]);
            obj.productId = Convert.ToInt32(dr[Billdetail.c_productId]);
            obj.numberOf = Convert.ToInt32(dr[Billdetail.c_numberOf]);
            obj.unitPriceUSD = Convert.ToDecimal(dr[Billdetail.c_unitPriceUSD]);
            obj.rate = Convert.ToDecimal(dr[Billdetail.c_rate]);
            obj.unitPriceVND = Convert.ToDecimal(dr[Billdetail.c_unitPriceVND]);
            obj.discount = Convert.ToDecimal(dr[Billdetail.c_discount]);
            obj.cash = Convert.ToDecimal(dr[Billdetail.c_cash]);
            obj.orderId = Convert.ToInt32(dr[Billdetail.c_orderId]);
            obj.productName = dr[Products.c_productName].ToString();
            return obj;
        }

        public List<Billdetail> getBillDetailByBillId(long billId)
        {
            List<Billdetail> list = new List<Billdetail>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "sp_getBillDetailByBillId";
            command.Parameters.Add("@billId", SqlDbType.BigInt).Value = billId;
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Insert(0, GetItem(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }

		public  List<Billdetail> GetAll()
		{
			return GetAll("select * from " + Billdetail.b_Billdetail, CommandType.Text);
		}

		public  Billdetail GetItemById(object id)
		{
			 return this.GetAll().FirstOrDefault(x => x.id == (long)id);
		}

        private List<Billdetail> GetAll(String strSQL, CommandType commandtype)
        {
            List<Billdetail> list = new List<Billdetail>();
            SqlConnection connection = connec.Connection;
            SqlCommand command = new SqlCommand();
            command.CommandType = commandtype;
            command.CommandText = strSQL;
            command.Connection = connection;
            connection.Open();
            SqlDataReader reader;
            using (reader = command.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    list.Insert(0, GetItem(reader));
                }
                reader.Close();
            }
            connection.Close();
            return list;
        }
	}

 }
